﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DynamicDNS.Enom;
using DynamicDNS.Model;
using System.Timers;
using System.ServiceModel;

namespace DynamicDNS.Wcf
{
    public class ConfigurationService : IConfigurationService
    {
        private static readonly ConfigurationService instance;
        public static ConfigurationService Instance { get { return instance; } }
        private Configuration Config;
        private ConfigurationContext Context = new ConfigurationContext();
        private Timer timer = new Timer();
        public bool Enable { get { return timer.Enabled; } set { timer.Enabled = value; } }
        private ConfigurationService()
        {
            Config = Context.Get();
            timer.Elapsed += timer_Elapsed;
            timer.Interval = 2000;
            timer.AutoReset = true;
            timer.Enabled = true;
        }

        void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            UpdateAll(false);
        }
        static ConfigurationService()
        {
            instance = new ConfigurationService();
        }

        public StartType GetStartType()
        {
            return Config.StartType;
        }

        public StartType SetStartType(StartType starttype)
        {
            Config.StartType = starttype;
            Context.Save(Config);
            return Config.StartType;
        }

        public List<Record> GetRecords()
        {
            return Config.Records;
        }

        public List<Record> SetRecord(Record record)
        {
            Config.Update(record);
            Context.Save(Config);
            return GetRecords();
        }

        public List<Record> DeleteRecord(Record record)
        {
            var presist = Config.Records.FirstOrDefault(r => r.Domain == record.Domain & r.Host == record.Host);
            Config.Records.Remove(presist);
            Context.Save(Config);
            return GetRecords();
        }


        public List<Record> UpdateRecord(Record record)
        {
            UpdatePresistRecord(record);
            return GetRecords();
        }

        public void UpdatePresistRecord(Record record)
        {
            var presist = Config.Records.FirstOrDefault(r => r.Domain == record.Domain & r.Host == record.Host);
            try
            {
                var service = new EnomService("http://dynamic.name-services.com/interface.asp");
                var setdnshost = new SetDNSHost(service, record.Host, record.Domain, record.Password);
                presist.IP = setdnshost.IP;
                presist.TimeStamp = DateTime.Now;
            }
            catch (Exception ex)
            {
                presist.IP = ex.Message;
            }
            Context.Save(Config);
        }

        public List<Record> UpdateAll(bool force)
        {
            foreach (var record in Config.Records)
            {
                var timespan = DateTime.Now - record.TimeStamp;
                var totalseconds = timespan.TotalSeconds;
                var interval = (int)record.Interval * 60;
                if (totalseconds > interval || force)
                    UpdatePresistRecord(record);
            }
            return GetRecords();
        }
    }
}
